package com.zhoujiayao.vision.service;

import cn.hutool.core.date.StopWatch;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.NumberUtil;
import com.zhoujiayao.VDetectionApi;
import com.zhoujiayao.dto.VDetectionInitConf;
import com.zhoujiayao.dto.VDetectionResult;
import com.zhoujiayao.vision.dto.PlateRecognitionRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class PlateServiceImpl implements PlateService {

    @Override
    public VDetectionResult plateRecognition(PlateRecognitionRequest request) {
        String imagePath = request.getImageInputPath();

        // 初始化
        VDetectionApi vDetectionApi = VDetectionApi.getInstance();
        VDetectionInitConf initConf = new VDetectionInitConf();
        boolean init = vDetectionApi.init(new VDetectionInitConf());

        // 检测图片
        VDetectionResult vDetectionResult = vDetectionApi.detectByImagePath(imagePath);

        // 释放资源（此接口不必每次调用，释放资源后需要重新初始化，项目运行过程中，只需要初始化一次即可，后续持续使用过程中不需要释放资源）
        return vDetectionResult;

    }

    @Override
    public List<VDetectionResult> plateRecognitionByFolder(PlateRecognitionRequest request) {
        // 初始化
        VDetectionApi vDetectionApi = VDetectionApi.getInstance();
        boolean init = vDetectionApi.init(new VDetectionInitConf());
        StopWatch stopWatch = new StopWatch();
        List<VDetectionResult> resultList = new ArrayList<>();
        stopWatch.start("开始读取图片并检测");
        long start = System.currentTimeMillis();
        List<File> fileList = FileUtil.loopFiles(request.getImageInputPath());
        for (File file : fileList) {
            VDetectionResult vDetectionResult = vDetectionApi.detectByImagePath(file.getAbsolutePath());
            resultList.add(vDetectionResult);
        }
        long allTime = System.currentTimeMillis() - start;
        stopWatch.stop();
        log.info("耗时:{}",stopWatch.prettyPrint(TimeUnit.MILLISECONDS));
        // 释放资源（此接口不必每次调用，释放资源后需要重新初始化，项目运行过程中，只需要初始化一次即可，后续持续使用过程中不需要释放资源）

        // 统计检测时间
        long totalTime = resultList.stream().map(VDetectionResult::getDetectionTime).reduce(0L, Long::sum);
        double avg = totalTime * 1.0 / resultList.size();
        long readImageTotalTime = allTime - totalTime;
        double avgReadImageTime = readImageTotalTime * 1.0 / resultList.size();
        double readProp = readImageTotalTime * 1.0 / totalTime * 100;
        log.info("全部检测时间（包括读取时间）：{} ms", allTime);
        log.info("检测图片数量：{}", resultList.size());
        log.info("检测总耗时：{} ms", totalTime);
        log.info("平均检测时间:{} ms",  NumberUtil.round(avg, 2));
        log.info("读取图片总时间：{} ms", NumberUtil.round(readImageTotalTime, 2));
        log.info("平均读取图片时间：{} ms", NumberUtil.round(avgReadImageTime, 2));
        log.info("读取图片时间占比：{} %", NumberUtil.round(readProp, 2));
        return resultList;
    }

}
